<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>	
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改</title>
<link rel="stylesheet" href="${ctx}/static/js/layui/css/layui.css">
<style type="text/css">
	.msg{
		color : red !important;
	}
</style>
</head>
<body>
	<div style="padding: 10px;">
		<form class="layui-form" action="">
			<input type="hidden" name="id" value="${param.id}">
			<div class="layui-form-item">
				<label class="layui-form-label">用户</label>
				<div class="layui-input-inline">
					<input type="text" name="username" required value="${param.username }"
						lay-verify="required" placeholder="请输入用户名" autocomplete="off"
						class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux msg"></div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">密码</label>
				<div class="layui-input-inline">
					<input type="password" name="password" required
						lay-verify="required" placeholder="请输入密码" autocomplete="off"
						class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux"></div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">性别</label>
				<div class="layui-input-block">
					<select name="sex" lay-verify="required">
						<option value="男" <c:if test="${param.sex eq '男'}">selected="selected"</c:if>>男</option>
						<option value="女" <c:if test="${param.sex eq '女'}">selected="selected"</c:if>>女</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">城市</label>
				<div class="layui-input-block">
					<select name="city" lay-verify="required">
						<option value="北京" checked="checked">北京</option>
						<option value="上海">上海</option>
						<option value="广州">广州</option>
						<option value="深圳">深圳</option>
						<option value="杭州">杭州</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">工作</label>
				<div class="layui-input-block">
					<input type="text" name="job" required lay-verify="required" value="程序员"
						placeholder="请输入工作" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">财富</label>
				<div class="layui-input-block">
					<input type="number" name="money" required lay-verify="required"
						placeholder="请输入财富" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
		<script src="${ctx}/static/js/layui/layui.js"></script>
		<script>
		//Demo
		layui.use('form', function(){
		  	var form = layui.form;
		  	var $ = layui.$;
		  
			//监听提交
			form.on('submit(formDemo)', function(data) {
				//$.post一般来说需要3个参数：url，参数，回调函数
				$.post('${ctx}/addUser', data.field , function (e){
					if (e == "") {
						parent.layui.table.reload("demo");
						parent.layer.msg('添加成功', {icon: 1});		//高用父窗口中的layer来提示，这样关闭弹出窗后提示不会立马关闭
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						parent.layer.close(index); //再执行关闭 
					} else {
						alert("失败");
					}
				})
				return false;
			});

			//光标离开后，去查询用户名是否存在
			$('input[name="username"]').blur(function() {
				var v = $(this).val();
				//Ajax请求
				$.ajax({
					url : '${ctx}/userIsExist', //地址
					data : { //data是参数
						username : v
					},
					success : function(e) { //成功后的回调函数
						if (e == "") {
							$('.msg').text("");
						} else {
							$('.msg').text(e);
							$('input[name="username"]').focus();
						}
					},
					error : function(xhr, status, error) { //失败后的回调函数
						console.log("失败：" + xhr);
					}
				})
			})
		});
		</script>
	</div>
</body>
</html>